package game;

import exceptions.InvalidLineIndexingException;
import interfaces.IHandler;

/**
 * Class, representing the MVC handler for the game, has a background game board
 * matrix, on which the logic is done
 * 
 * @author Home
 * 
 */
public class GameHandler implements IHandler {

	private char[][] gameBoard;
	private int boardSize;
	private int emptyCellsCount;

	public GameHandler(int boardSize) {

		setBoardSize(boardSize);
		gameBoard = new char[boardSize][boardSize];
		initializeGameBoard();
		emptyCellsCount = (int) Math.pow(boardSize, 2);

	}

	@Override
	public int getRowsCount() {
		// TODO Auto-generated method stub
		return boardSize;
	}

	@Override
	public int getColsCount() {
		// TODO Auto-generated method stub
		return boardSize;
	}

	@Override
	public String getStringForRowAndCol(int row, int col) {

		return String.valueOf(gameBoard[row][col]);

	}

	/**
	 * Fills a line in the matrix
	 * 
	 * @param line
	 *            - the line which the player selected for drawing
	 * @return whether the player has scored a point or not
	 * @throws InvalidLineIndexingException
	 */
	public boolean updateGameBoard(Line line, String sign)
			throws InvalidLineIndexingException {
		/*
		 * some code, calculating row and column for the line ...
		 */
		return hasScored(0, 0/* row, col */, sign);

	}

	/*
	 * determines if the player has scored after filling line, and if so, fills
	 * the players sign in the array
	 */
	private boolean hasScored(int row, int col, String sign) {
		/*
		 * --some code-- freeCellsCount --;
		 */
		return false;
	}

	/*
	 * initializes the initial state of the gameBoard field
	 */
	private void initializeGameBoard() {
		/*
		 * initialize initial state of matrix
		 */
	}

	/*
	 * validates and then sets the value of boardSize field
	 */
	private void setBoardSize(int boardSize) {

		if (boardSize > 0) {
			this.boardSize = boardSize;
		}

	}

	/**
	 * 
	 * @return empty cells count on the game board
	 */
	public int getEmptyCellsCount() {
		return emptyCellsCount;
	}

	public int getBoardSize() {
		return boardSize;
	}

}
